package com.rh.number.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.NoArgsConstructor;

import javax.sql.rowset.CachedRowSet;
import java.util.Collections;
import java.util.List;

@NoArgsConstructor(force = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PageResult<T> {
    private final int code;
    private final String msg;
    private final List<T> data;
    private final int currentPage;
    private final int pageSize;
    private final long totalElements;
    private final int totalPages;



    public int getCode() { return code; }
    public String getMsg() { return msg; }
    public List<T> getData() { return data; }
    public int getCurrentPage() { return currentPage; }
    public int getPageSize() { return pageSize; }
    public long getTotalElements() { return totalElements; }
    public int getTotalPages() { return totalPages; }

    private PageResult(int code, String msg, List<T> data,
                       int currentPage, int pageSize,
                       long totalElements, int totalPages) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalElements = totalElements;
        this.totalPages = totalPages;
    }

    public static <T> PageResult<T> success(List<T> data, int currentPage,
                                            int pageSize, long totalElements,
                                            int totalPages) {
        return new PageResult<>(200, "success", data,
                currentPage, pageSize,
                totalElements, totalPages);
    }

    public static <T> PageResult<T> error(int code, String msg) {
        return new PageResult<>(code, msg, null, 0, 0, 0, 0);
    }

    // 新增单结果成功方法
    public static <T> PageResult<T> singleSuccess(T data) {
        return new PageResult<>(
                200, "success",
                Collections.singletonList(data),
                1, 1, 1, 1
        );
    }

    public CachedRowSet setPageNum(T pageNum) {
        return (CachedRowSet) pageNum;
    }
}